namespace net.podhola.gtd.controllers

import System
import System.Configuration
import System.Globalization
import System.Web
import System.Web.UI
import System.Web.UI.WebControls
import Castle.MonoRail.Framework
import net.podhola.gtd.finances

/* Scaffolding( PlannedIncome ) */
[Layout("default")]
class PlannedIncomesController(GTDParentController):

	override def Title():		
		return "Incomes"

	def FindIncome( id as int ) as PlannedIncome:
		PropertyBag["Income"] = PlannedIncome.Find( id )
		return PropertyBag["Income"]

	def Detail( id as int ):
		FindIncome( id )

	def Edit( id as int ):
		FindIncome( id )

	def New():
		pass

	def Receive( id as int ):
		FindIncome( id )

	def Delete( id as int ):
		FindIncome( id ).Delete()
		RedirectToAction("Index" )

	def Create( Subject, Amount, ExpectedIncomeDate, PlannedAccount ):
		Income = PlannedIncome()
		accountid as int = Int32.Parse( Form[ "PlannedAccount" ] )
		Income.PlannedAccount = Account.Find( accountid )
		
		amount as double = Double.Parse( Form[ "Amount" ], NumberStyles.Currency )
		Income.PlannedAmount = amount
		Income.Subject = Subject
		Income.ExpectedIncomeDate  = DateTime.Parse( ExpectedIncomeDate )			
		
		Income.Create()

		PropertyBag["Income"] = Income

	def Update( [DataBind("Income")] Income as PlannedIncome ):
		accountid as int = Int32.Parse( Form[ "Income.PlannedAccount" ] )
		Income.PlannedAccount = Account.Find( accountid )
		amount as double = Double.Parse( Form[ "Income.PlannedAmount" ], NumberStyles.Currency )
		Income.PlannedAmount = amount
		Income.Save()
		RedirectToAction("Detail", { "id":Income.Id } )

	def GetPlannedIncome() as double:
		amountDue as double = 0.0
		allIncomes = PlannedIncome.FindAll()
		for Income in allIncomes:
			amountDue = amountDue + Income.PlannedAmount - Income.GetAlreadyReceived()
		return amountDue

	override def GetWelcomeNumber() as string:
		return GetPlannedIncome().ToString("c")
		
	override def GetWelcomeText() as string:
		return "expected"
